﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//
using System.IO;
using System.IO.Ports;
using System.Windows.Forms;
using System.Web;

namespace iPublic
{
    /// <summary>
    /// 公共定义，海宏软件
    /// 修改记录：
    ///   20171212，zch
    /// </summary>
    public partial class iDefine
    {
        public static bool debugMode = false;

        public static _SysConfig SysConfig = new _SysConfig();
        public class _SysConfig
        {
            //机器号
            public string machineID = "001";
            public string sMacAddress = "";
            public string sStatus = "正常";
            //用户号
            public string sUserID() { return fUserID; }
            public string fUserID = "";      //ID
            public string sUserName = "";    //Name
            //摄像头相关
            public string sCameraID = "";
            public string sCameraFenBianLv = "";
            //控制器
            public string sComPort = "";
            public int nComBaudRate = 9600;
            //广告
            public bool guanggaoEnable = true;  //广告生效
            //其他
            public string sBaiduKey = "hhCEw1lxrR6yR8NDyKLBfXgcQysdUwPP";   //李建飞申请的
            public int readConfig(string AppConfigFileName = "")
            {
                int result = -1;
                if (string.IsNullOrEmpty(AppConfigFileName)) AppConfigFileName = "App.Config";
                string sFile = findSysFile(AppConfigFileName);
                //摄像头
                sCameraID = gsClass.getAppConfigStr("摄像头.ID", "", sFile);
                sCameraFenBianLv = gsClass.getAppConfigStr("摄像头.分辨率", sCameraFenBianLv, sFile);
                //控制器
                sComPort = gsClass.getAppConfigStr("控制器.串口号", "", sFile);
                nComBaudRate = (int)gsClass.getAppConfigNumber("控制器.波特率", nComBaudRate, sFile);
                if (nComBaudRate <= 0) nComBaudRate = 9600;
                //
                return result;
            }
        }

        /// <summary>
        /// 公共的串口操作组件
        /// </summary>
        public static iComHelper comHelper = null;


        #region //ToufangItem，ToufangItemList，投放物品清单
        public static ToufangItemList toufangList = null;
        //public class ToufangItem : json验证物品 { }
        public class ToufangItemList : List<json验证物品>
        {
            public json绑定用户增加积分 A绑定用户增加积分 = null;
        }
        #endregion


        #region //积分绑定用户通过用户名为用户增加积分。{"result":true,"add_score":100}
        public class json绑定用户增加积分
        {
            public string result = "";
            public double add_score = 0;
        }
        #endregion


        #region //json验证物品，阿里识别后发给自己的服务器识别是否可留，返回结果
        public class json验证物品
        {   //{"reslut":"0","msg":"不属于回收物品"}
            public string result = "";
            public string msg = "";
        }
        #endregion


        #region //weixinInpuUserResult，微信网页授权登陆后的服务器返回值
        public class weixinInpuUserResult
        {
            public string result = "";
            public string user_id = "";
        }
        #endregion


        #region //jsonShibieErr，jsonShibieItem，jsonShibieOK：识别的相关定义
        //识别错误：{"msg":"Missing argument: img_file/img_url/img_base64","code":1003}
        public class jsonShibieErr
        {
            public string msg = "";
            public string code = "";
        }

        //识别正确：{"result":[{"id":"664","probability":0.2197652906179428,"description":"显示器","description_en":"monitor"},{"id":"1341","probability":0.13619302213191986,"description":"显示器","description_en":"display, video display"}],"final_time":1111}
        public class jsonShibieItem
        {   //一行条目：{"id":"664","probability":0.2197652906179428,"description":"显示器","description_en":"monitor"}
            public string id = "";
            public double probability = 0;
            public string description = "";
            public string description_en = "";
        }
        public class jsonShibieOK
        {
            public List<jsonShibieItem> result = null;
            public int final_time = 0;
        }
        #endregion


        public static json广告清单 guanggao = null;

        #region //json广告清单，从服务器get_ad下载的广告清单
        /// <summary>
        /// 从服务器get_ad下载的广告清单
        /// </summary>
        public class json广告清单
        {   //{"total":3,"per_page":4,"current_page":1,"data":[{"ad_id":15,"ad_title":"11","ad_dec":"111","ad_img":"http:\/\/www.aigou198.com\/uploads\/20171212\/01b6c075fdc17cb7a34c36757d90987e.jpg","status":"是","product_id":"","ad_start":1513047699,"ad_end":1576292501,"ad_create":1513047720,"ad_del":"1"},{"ad_id":16,"ad_title":"感谢海宏","ad_dec":"感谢海宏软件！","ad_img":"http:\/\/www.aigou198.com\/uploads\/20171216\/d2cffabb0bbd6b6b1fc84321e7cc893c.jpg","status":"是","product_id":"","ad_start":1513409066,"ad_end":1608881069,"ad_create":1513409096,"ad_del":"1"},{"ad_id":17,"ad_title":"测试广告","ad_dec":"","ad_img":"http:\/\/www.aigou198.com\/uploads\/20171216\/ddd131bfb6f0b3d47d60d9659889a1d9.jpg","status":"是","product_id":"","ad_start":1513409113,"ad_end":1575530715,"ad_create":1513409125,"ad_del":"1"}]}
            public int total = 0;
            public int per_page = 0;
            public int current_page = 0;
            public List<One广告> data = null;
            public class One广告
            {   //{"ad_id":16,"ad_title":"感谢海宏","ad_dec":"感谢海宏软件！","ad_img":"http:\/\/www.aigou198.com\/uploads\/20171216\/d2cffabb0bbd6b6b1fc84321e7cc893c.jpg","status":"是","product_id":"","ad_start":1513409066,"ad_end":1608881069,"ad_create":1513409096,"ad_del":"1"}
                public string ad_id = "";           //16
                public string ad_title = "";        //感谢海宏
                public string ad_dec = "";          //感谢海宏软件
                public string ad_img = "";          //http:\/\/www.aigou198.com\/uploads\/20171216\/d2cffabb0bbd6b6b1fc84321e7cc893c.jpg
                public string status = "";          //是
                public string product_id = "";      //
                public int ad_start = 0;            //1513409066
                public int ad_end = 0;              //1608881069
                public int ad_create = 0;           //1513409096
                public string ad_del = "";          //1
            }
        }
        #endregion


        #region //jsonBaiduGPS地址，百度获取GPS地址返回的JSON
        /// <summary>
        /// baidu返回的json
        /// </summary>
        public class jsonBaiduGPS地址
        {
            /*分析返回值，可以从这里看json解析：https://www.bejson.com
                {   "address":"CN|\u5c71\u4e1c|\u6f4d\u574a|None|CHINANET|0|0",
                    "content":{ "address":"\u5c71\u4e1c\u7701\u6f4d\u574a\u5e02",
                        "address_detail":{ "city":"\u6f4d\u574a\u5e02","city_code":287,"district":"",
                        "province":"\u5c71\u4e1c\u7701","street":"","street_number":""},
                     "point":{ "x":"119.14263382","y":"36.71611487"} 
                 },"status":0} */
            public string address = "";             //CN|山东|潍坊|None|CHINANET|0|0
            public _content content = null;
            public class _content
            {
                public string address = "";         //山东省潍坊市
                public _address_detail address_detail = null;
                public class _address_detail
                {
                    public string city = "";        //潍坊市
                    public string city_code = "";   //287
                    public string district = "";    //
                    public string province = "";    //山东省
                    public string street = "";      //
                    public string street_number = "";
                }
                public _point point = null;
                public class _point
                {
                    public string x = "";           //119.14263382
                    public string y = "";           //36.71611487
                }
            }
            public string status = "";              //0
            public void decode()
            {   //反解出来中文
                address = HttpUtility.UrlDecode(address);
                content.address = HttpUtility.UrlDecode(content.address);
                content.address_detail.city = HttpUtility.UrlDecode(content.address_detail.city);
                content.address_detail.province = HttpUtility.UrlDecode(content.address_detail.province);
            }
        }
        #endregion


        //提示信息
        public delegate void showInfoProc(string sInfo, bool lTime = true);

        #region //ChuLiJieGuo，通用的处理结果返回结构
        /// <summary>
        /// 通用的处理结果返回结构
        /// </summary>
        public class ChuLiJieGuo
        {
            public int error = -1;                      //成功返回0
            public string state = "";                   //备用的，个人习惯
            public string message = "";                 //返回的文字信息
            //public object[] data = new object[2];
            public object data = null, data2 = null, data3 = null, data4 = null, data5 = null;  //随意返回点什么
            public string callBack = "";                //回调函数
        }
        #endregion


        #region //findAppConfigFile，查找配置文件名
        public static string findAppConfigFile() { return findAppConfigFile(""); }
        public static string findAppConfigFile(string AExeFileName)
        {
            string s = AExeFileName;
            if (string.IsNullOrEmpty(s)) s = Path.GetFileName(Application.ExecutablePath) + ".Config";
            string sPath = System.Environment.CurrentDirectory;    //System.IO.Path.GetDirectoryName(iDefine.findSysDirectory("SysData", "", 0))
            string sFile = sPath + "\\" + s;
            if (!System.IO.File.Exists(sFile)) sFile = iDefine.findSysFile(s);  //尝试往上查找
            if (!File.Exists(sFile)) sFile = iDefine.findSysFile("App.Config"); //尝试查找App.Config
            if (!File.Exists(sFile))
            {   //都没有，则自动创建一个空的
                sFile = sPath + "\\App.Config";
                File.CreateText(sFile).Close();
            }
            return sFile;
        }
        #endregion


        #region //查找系统路径和文件，findSysFile、findSysDirectory、getRootPath
        public static string getRootPath() { return Path.GetDirectoryName(findSysDirectory()); }
        public static string findSysDirectory() { return findSysDirectory("SysData", "", -1); }
        public static string findSysDirectory(string sDirName, string s最深层路径, int n搜索深度)
        {
            string result = "", s = s最深层路径;
            if (string.IsNullOrEmpty(s))
            {   //判断是cs还是bs程序
                if (HttpContext.Current != null)
                    s = HttpContext.Current.Server.MapPath("~/");   //HttpContext.Current.Request.PhysicalApplicationPath
                else
                    s = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath); //System.Environment.CurrentDirectory;
            }
            if (string.IsNullOrEmpty(sDirName)) sDirName = "SysData";
            int nMax = n搜索深度;
            if (nMax < 1) nMax = 6;
            //循环查找文件夹
            for (int i = 1; i <= nMax; i++)
            {
                string s2 = s + "\\" + sDirName;
                if (Directory.Exists(s2 + "\\"))
                {
                    result = s2;
                    break;
                }
                else
                {
                    s = Path.GetDirectoryName(s);           //上一级
                }
            }
            //
            return result;
        }
        public static string findSysFile(string AFileName) { return findSysFile(AFileName, "", -1); }
        public static string findSysFile(string AFileName, string s最深层路径, int n搜索深度)
        {
            string result = "", s = s最深层路径;
            //System.Environment.CurrentDirectory;
            if (string.IsNullOrEmpty(s))
            {   //判断是cs还是bs程序
                if (HttpContext.Current != null)
                    s = HttpContext.Current.Server.MapPath("~/");
                else
                    s = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);
            }
            if (string.IsNullOrEmpty(AFileName)) AFileName = "App.Config";
            int nMax = n搜索深度;
            if (nMax < 1) nMax = 6;
            //循环查找文件夹
            for (int i = 1; i <= nMax; i++)
            {
                string s2 = s + "\\" + AFileName;
                if (File.Exists(s2))
                {
                    result = s2;
                    break;
                }
                else
                {
                    s = Path.GetDirectoryName(s);           //上一级
                }
            }
            //
            return result;
        }
        #endregion

    }

}
